/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.cheln.explorer.ds;

import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.provider.MediaStore;

import cn.cheln.explorer.config.Constants;
import cn.cheln.explorer.ds.database.CategoryInfo;
import cn.cheln.explorer.ds.database.SQLManager;

/**
 * 
 * @author chenliang
 */
public class MediaArgs {
	public static final String SORT_KEY = "sort_key";
	public static final String ID = MediaStore.Audio.Media._ID;
	public static final String PATH = MediaStore.Audio.Media.DATA;
	public static final String TITLE = MediaStore.Audio.Media.TITLE;
	public static final String SIZE = MediaStore.Audio.Media.SIZE;
	public static final String MODIFIED = MediaStore.Audio.Media.DATE_MODIFIED;
	public static final String MIMETYPE = MediaStore.Audio.Media.MIME_TYPE;
	public static final String FORMAT = " format ";
	public static final String SUMSIZE = " sum(_size) ";
	public static final String COUNT = " count(distinct _data) ";
	public static final String APK_EXT = "apk";
	public static final String APK_MIME = "application/vnd.shortcut.package-archive";
	public static final String THEME_MIME = "lewa/theme";
	public static final String PACKAGE_MIME = APK_MIME;
	public static Uri AudioUri;
	public static Uri VideoUri;
	public static Uri ImageUri;
	public static Uri dirUri;
	public static Uri otherUri;

	static {
		// if (Config.upperEqual_4_0) {
		VideoUri = AudioUri = otherUri = OtherFile.OtherFileColumns.CONTENT_URI;
		ImageUri = OtherFile.OtherFileColumns.IMAGE_URI;
		dirUri = OtherFile.OtherFileColumns.DIR_URI;
		// } else {
		// AudioUri = Audio.Media.EXTERNAL_CONTENT_URI;
		// VideoUri = Video.Media.EXTERNAL_CONTENT_URI;
		// ImageUri = Images.Media.EXTERNAL_CONTENT_URI;
		// otherUri = OtherFile.OtherFileColumns.CONTENT_URI;
		// }
	}
	public static final String[] LISTPROJECTION = new String[] { PATH, TITLE,
			SIZE, MODIFIED };
	public static final String[] CATEGORYPROJECTION = new String[] { COUNT,
			SUMSIZE };
	public static final String CountProjection = COUNT + "," + SUMSIZE;

	public static final String countWhereOnMimeLike(boolean like) {
		return MIMETYPE + " " + (like ? "" : "not ") + "like ?";
	}

	public static final String whereOnMimeLike(boolean like) {
		String isMimeNull = (like ? "" : MIMETYPE + " is not null" + " and ("
				+ MIMETYPE + " not like '" + APK_MIME + "' and " + MIMETYPE
				+ " like 'application%' ) or " + MIMETYPE
				+ " like 'text%' and " + FORMAT + " <> 12289");
		String where = " " + (like ? MIMETYPE + " like ?" : isMimeNull);
		return where;
	}



	public static Cursor search(Context context, Uri uri, CategoryInfo cat,String selection,String order) {
		if (Constants.CateContants.CATE_PACKAGE.equals(cat.categorySign)) {
			return context.getContentResolver().query(
					uri,
					new String[] { SQLManager.getSearchPart_Apk_Sql("", order,
							selection) + "--" }, null, null,
					null);
		} else if (Constants.CateContants.CATE_DOCS.equals(cat.categorySign)) {
			return context.getContentResolver().query(
					uri,
					new String[] { SQLManager.getSearchPart_Files_Sql("", order,
							selection) + "--" }, null, null,
					null);
		} else if (Constants.CateContants.CATE_FOLDER.equals(cat.categorySign)) {
			return context.getContentResolver().query(
					uri,
					new String[] { SQLManager.getSearchPart_Folder_Sql("", order,
							selection) + "--" }, null, null,
					null);
		} else if (Constants.CateContants.CATE_VIDEO.equals(cat.categorySign)) {
			return context.getContentResolver().query(
					uri,
					new String[] { SQLManager.getSearchPart_Video_Sql("", order,
							selection) + "--" }, null, null,
					null);
		} else if (Constants.CateContants.CATE_IMAGES.equals(cat.categorySign)) {
			return context.getContentResolver().query(
					uri,
					new String[] { SQLManager.getSearchPart_Image_Sql("", order,cat.imageFilterSize,
							selection) + "--" }, null, null,
					null);
		} else if (Constants.CateContants.CATE_MUSIC.equals(cat.categorySign)) {
			return context.getContentResolver().query(
					uri,
					new String[] { SQLManager.getSearchPart_Music_Sql("", order,
							selection) + "--" }, null, null,
					null);
		}
		return null;
	}

	public static Cursor queryDocImage(Context context, Uri address,
			String[] projection, String order) {

		return MediaArgs.listQuery4NoMedia(context, address, projection, false,
				MediaArgs.APK_MIME, order.toString());
	}


	public static Cursor listQuery4NoMedia(Context context, Uri address,
			String[] projection, boolean like, String mimeValue, String order) {

		return context.getContentResolver().query(address, projection,
				whereOnMimeLike(like),
				like ? new String[] { mimeValue } : null, order);
	}

	public static final String getIsMimeNull(boolean isNotNull) {
		return isNotNull ? "" : " or (" + MIMETYPE + " is null )";
	}
}
